﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Xml;
using JsonFx.Json;

namespace AuctionHouseDataLibrary
{
    public static class ItemMangement
    {
        public const string WowheadItemXmlUrl = "http://www.wowhead.com/item={0}&xml";
        public const string WowheadItemIconUrl = "http://wow.zamimg.com/images/wow/icons/large/{0}.jpg";
        public const string WowheadItemIconFilename = "{0}\\{1}.jpg";

        public static void CheckDownloadThumbnail(string icon)
        {
            string filename = string.Format(WowheadItemIconFilename, Globals.ImagesFolder, icon);
            string iconUrl = string.Format(WowheadItemIconUrl, icon);
            if (!File.Exists(filename))
            {
                WebClientCustom.DownloadFile(iconUrl, filename);
            }
        }

        public static void GetItemFromWoWhead(int itemId, Item returnItem)
        {
            string wowheadUrl = string.Format(WowheadItemXmlUrl, itemId);
            string itemDownload = WebClientCustom.DownloadString(wowheadUrl);
            XmlDocument itemXml = new XmlDocument();
            itemXml.LoadXml(itemDownload);

            XmlNode name = itemXml.SelectSingleNode("//name");
            XmlNode itemLevel = itemXml.SelectSingleNode("//level");
            XmlNode quality = itemXml.SelectSingleNode("//quality");
            XmlNode classNode = itemXml.SelectSingleNode("//class");
            XmlNode subclass = itemXml.SelectSingleNode("//subclass");
            XmlNode icon = itemXml.SelectSingleNode("//icon");
            XmlNode inventorySlot = itemXml.SelectSingleNode("//inventorySlot");
            XmlNode json = itemXml.SelectSingleNode("//json");
            XmlNode jsonEquip = itemXml.SelectSingleNode("//jsonEquip");

            if (name != null && itemLevel != null && quality != null && classNode != null && subclass != null && icon != null
                && inventorySlot != null && json != null)
            {
                returnItem.ItemId = itemId;
                returnItem.ItemName = name.InnerText;

                returnItem.InventorySlotDesc = inventorySlot.InnerText;
                returnItem.InventorySlotId = Int32.Parse(inventorySlot.Attributes["id"].Value);

                returnItem.ClassId = Int32.Parse(classNode.Attributes["id"].Value);
                returnItem.ClassDesc = classNode.InnerText;

                returnItem.DisplayId = Int32.Parse(icon.Attributes["displayId"].Value);
                returnItem.DisplayIcon = icon.InnerText.ToLower();

                returnItem.ItemLevel = Int32.Parse(itemLevel.InnerText);

                returnItem.QualityId = Int32.Parse(quality.Attributes["id"].Value);
                returnItem.QualityDesc = quality.InnerText;

                returnItem.SubclassId = Int32.Parse(subclass.Attributes["id"].Value);
                returnItem.SubclassDesc = subclass.InnerText;

                if (jsonEquip != null)
                {
                    dynamic jsonEquipDynamic = new JsonReader().Read("{ " + jsonEquip.InnerText + " }");
                    if (jsonEquip.InnerText.Contains("sellprice"))
                        returnItem.SellPrice = jsonEquipDynamic.sellprice;
                }
                if (json != null)
                {
                    dynamic jsonDynamic = new JsonReader().Read("{ " + json.InnerText + " }");
                    if (json.InnerText.Contains("reqlevel"))
                        returnItem.RequiredLevel = jsonDynamic.reqlevel;
                }

                if (!string.IsNullOrEmpty(returnItem.DisplayIcon))
                    CheckDownloadThumbnail(returnItem.DisplayIcon);

                returnItem.DataUpdated = true;
                Globals.Logger.InfoFormat("Item {0} updated.", itemId);
            }
            else
            {
                Globals.Logger.WarnFormat("Item {0} unable to parse.", itemId);    
            }
        }
    }
}